package cn.wsalix.shop.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Observable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.admin.BaseUser;
import cn.anlaser.form.UploadFileForm;
import cn.anlaser.service.impl.SimpleServiceImpl;
import cn.anlaser.status.FlagEnum;
import cn.anlaser.type.FileTypeEnum;
import cn.anlaser.type.ImageTypeEnum;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.observable.service.FileUploadService;
import cn.wsalix.shop.entity.Brand;
import cn.wsalix.shop.form.BrandForm;
import cn.wsalix.shop.repository.BrandRepository;
import cn.wsalix.shop.service.BrandService;

@Service("brandService")
public class BrandServiceImpl extends
		SimpleServiceImpl<BrandForm, BrandRepository, Brand> implements
		BrandService {
	@Autowired
	private BrandRepository brandRepository;

	@Override
	public String module() {
		return "/admin/brand";
	}

	@Override
	public BrandRepository getRepository() {
		return brandRepository;
	}

	@Override
	public Class<Brand> getEntityClazz() {
		return Brand.class;
	}

	@Transactional
	@Override
	public Brand getNew() {
		BaseUser user = userUtils.getUser();
		BrandForm form = new BrandForm();
		form.setFlag(FlagEnum.create);
		form.setCreatedBy(user.getId());
		List<Brand> resLst = findByForm(form);
		if (!resLst.isEmpty()) {
			return resLst.get(0);
		} else {
			Brand product = new Brand();
			return add(product);
		}

	}

	@Override
	public Brand add(BrandForm form) {
		Brand obj = brandRepository.findOne(form.getId());
		traversalAssign(form, obj);
		obj.setFlag(FlagEnum.valid);
		return add(obj);
	}

	@Override
	public Map<String, SearchFilter> buildByForm(BrandForm form) {
		Map<String, SearchFilter> filters = buildFilter(form);
		if (form.getName() != null && !form.getName().trim().equals("")) {
			filters.put("name",
					new SearchFilter("name", Operator.LIKE, form.getName()));
		}
		return filters;
	}

	/*
	 * @Override public void update(Observable o, Object arg) { if (o instanceof
	 * ProductChangeService) { ProductForm form = (ProductForm) arg; BrandForm
	 * brandForm = new BrandForm(); brandForm.setProductId(form.getId());
	 * brandForm.setBrand(form.getBrand());
	 * brandForm.setBrandLogo(form.getBrandLogo()); edit(brandForm); } }
	 */
	@Transactional
	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		super.delete(id);
	}

	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof FileUploadService) {
			// FileUploadService fileUploadService = (FileUploadService) o;
			if (arg instanceof UploadFileForm) {
				UploadFileForm obj = (UploadFileForm) arg;
				if (obj.getType().ordinal() == FileTypeEnum.image.ordinal()) {
					if (obj.getImageType().equals(ImageTypeEnum.brand.name())) {
						if (obj.getFkId() != 0) {
							Brand brand = new Brand();
							brand.setName(obj.getName());
							brand.setLogo(obj.getHttpUrl());
							super.add(brand);
						} else {
							// edit
							Brand brand = brandRepository
									.findOne(obj.getFkId());
							brand.setLogo(obj.getHttpUrl());
							super.edit(brand);
						}

					}
				}
			}
		}

	}

}
